﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.Model.Entity;
using PPAP.BO;

namespace PPAP.Web.SearchReport
{
    public partial class SamplingInspection : PageBase
    {
        PPARBO bo = new PPARBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bindDepartment();
                bindData();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            bindData();
        }

        public void bindData()
        {
            PPAP.Model.Search.SamplingInspectionSearch sis = new Model.Search.SamplingInspectionSearch();
            sis.PartNo = this.tbPartno.Text;
            sis.SupplierNo = this.tbSupplierNo.Text;
            if (this.dpStartTime1.Text != "")
                sis.StartTime1 = base.ToDateTime(this.dpStartTime1.Text, "error format!");
            if (this.dpStartTime2.Text != "")
                sis.StartTime2 = base.ToDateTime(this.dpStartTime2.Text, "error format!");
            if (this.dpEndTime1.Text != "")
                sis.EndTime1 = base.ToDateTime(this.dpEndTime1.Text, "error format!");
            if (this.dpEndTime2.Text != "")
                sis.EndTime2 = base.ToDateTime(this.dpEndTime2.Text, "error format!");
            if (this.ddlDept.SelectedValue != "")
                sis.DepartmentID = base.ToInt32(this.ddlDept.SelectedValue, "error format!");
            sis.DeptRep = this.tbDeptRep.Text;
            if (this.ddlStatus.SelectedValue != "")
                sis.Status = base.ToInt32(this.ddlStatus.SelectedValue, "error format!");
            if (this.ddlResult.SelectedValue != "")
                sis.Result = this.ddlResult.SelectedValue;
            sis.MyCase = this.chkMycase.Checked;
            sis.CreateUser = CurrentUser.Id;

            DataTable dt = bo.GetSamplingInspection(sis).Tables[0];
            //dt.Columns.Add("ShowStatus");
            foreach (DataRow r in dt.Rows)
            {
                r["ShowStatus"] = ShowSampleCardStatus(r["status"].ToString());
            }
            AspNetPager2.RecordCount = dt.Rows.Count;
            dgList.DataSource = GetPageTable(dt, AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            dgList.DataBind();
        }

       

        //状态显示
        public string ShowSampleCardStatus(string Status)
        {
            string result = "";
            result = "";

            switch (Status.ToLower())
            {

                case "0":
                    result = "Created";
                    break;
                case "1":
                    result = "Circulating";
                    break;
                case "2":
                    result = "Circulating(<font color='red'>Reject</font>)";
                    break;
                default:
                    result = "Decided";
                    break;
            }
            return result;
        }

        private void bindDepartment()
        {
            TestDepartmentBO tdbo = new TestDepartmentBO();
            DataTable dt = tdbo.GetList("").Tables[0];
            ListItem item;
            item = new ListItem("All...", "0");
            this.ddlDept.Items.Add(item);
            foreach (System.Data.DataRow row in dt.Rows)
            {
                item = new ListItem(row["TESTITEMNAME"].ToString(), row["ID"].ToString());
                this.ddlDept.Items.Add(item);
            }
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            btnSubmit_Click(null, null);
        }

        /// <summary>
        /// Export
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            PPAP.Model.Search.SamplingInspectionSearch sis = new Model.Search.SamplingInspectionSearch();
            sis.PartNo = this.tbPartno.Text;
            sis.SupplierNo = this.tbSupplierNo.Text;
            if (this.dpStartTime1.Text != "")
                sis.StartTime1 = base.ToDateTime(this.dpStartTime1.Text, "error format!");
            if (this.dpStartTime2.Text != "")
                sis.StartTime2 = base.ToDateTime(this.dpStartTime2.Text, "error format!");
            if (this.dpEndTime1.Text != "")
                sis.EndTime1 = base.ToDateTime(this.dpEndTime1.Text, "error format!");
            if (this.dpEndTime2.Text != "")
                sis.EndTime2 = base.ToDateTime(this.dpEndTime2.Text, "error format!");
            if (this.ddlDept.SelectedValue != "")
                sis.DepartmentID = base.ToInt32(this.ddlDept.SelectedValue, "error format!");
            sis.DeptRep = this.tbDeptRep.Text;
            if (this.ddlStatus.SelectedValue != "")
                sis.Status = base.ToInt32(this.ddlStatus.SelectedValue, "error format!");
            if (this.ddlResult.SelectedValue != "")
                sis.Result = this.ddlResult.SelectedValue;
            sis.MyCase = this.chkMycase.Checked;
            sis.CreateUser = CurrentUser.Id;

            DataSet ds = bo.GetSamplingInspection(sis);
            //ds.Tables[0].Columns.Add("ShowStatus");
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                r["ShowStatus"] = ShowSampleCardStatus(r["status"].ToString());
                r["d1"] = r["starttime"].ToString() == "" ? r["starttime"] : DateTime.Parse(r["starttime"].ToString()).ToString("yyyy-MM-dd");
                r["d2"] = r["endtimeplan"].ToString() == "" ? r["endtimeplan"] : DateTime.Parse(r["endtimeplan"].ToString()).ToString("yyyy-MM-dd");
            }
            ds.Tables[0].Columns.Remove("ID");
            ds.Tables[0].Columns.Remove("rid");
            //ds.Tables[0].Columns.Remove("SAMPLECARDNO");
           // ds.Tables[0].Columns.Remove("VERSION");
          //  ds.Tables[0].Columns.Remove("TESTDEPARTMENTID");
           // ds.Tables[0].Columns.Remove("DEPTREP");
           // ds.Tables[0].Columns.Remove("ENDTIMEACTUAL");
            ds.Tables[0].Columns.Remove("STATUS");
            ds.Tables[0].Columns.Remove("starttime");
            ds.Tables[0].Columns.Remove("endtimeplan");
            //ds.Tables[0].Columns.Remove("REASON");
            //ds.Tables[0].Columns.Remove("INTERNALCOMMENT");
           // ds.Tables[0].Columns.Remove("INSPECTIONREPORTID");
            ds.Tables[0].Columns["ShowStatus"].ColumnName = "Status";

            ds.Tables[0].Columns["ROWNUM"].ColumnName = "No.";
            ds.Tables[0].Columns["PPARNO"].ColumnName = "PPAR.No";
            ds.Tables[0].Columns["BDNTPARTNO"].ColumnName = "Part.No";
            ds.Tables[0].Columns["SUPPLIERCODE"].ColumnName = "Supplier Code";
            ds.Tables[0].Columns["SAMPLECARDNO"].ColumnName = "Sample card No.";
            ds.Tables[0].Columns["TESTITEMNAME"].ColumnName = "Department";
            ds.Tables[0].Columns["RESULT"].ColumnName = "Result";
            ds.Tables[0].Columns["d1"].ColumnName = "Start date";
            ds.Tables[0].Columns["d2"].ColumnName = "End date";
            //ds.Tables[0].Columns["testitemname"].ColumnName = "Dept.rep";
            CreateExcel(ds, "SamplingInspection");
        }
    }
}